package com.dcjt.firehotcarpad.base

import android.app.Activity
import android.content.Context
import com.aries.library.fast.i.*
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastError
import com.aries.library.fast.widget.FastLoadDialog
import com.aries.library.fast.widget.FastLoadMoreView
import com.aries.library.fast.widget.FastMultiStatusView
import com.aries.ui.widget.progress.UIProgressView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.loadmore.LoadMoreView
import com.dcjt.firehotcarpad.R
import com.marno.easystatelibrary.EasyStatusView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.header.ClassicsHeader


class AppImpl(private val mContext: Context): DefaultRefreshHeaderCreater
    , LoadMoreFoot, MultiStatusView, LoadingDialog, HttpErrorControl {
    private val TAG = this.javaClass.simpleName


    /**
     * 下拉刷新头配置
     *
     * @param context
     * @param layout
     * @returnk
     */
    override fun createRefreshHeader(context: Context, layout: RefreshLayout): RefreshHeader {
        layout.setEnableHeaderTranslationContent(true)
        return ClassicsHeader(mContext)
    }

    /**
     * Adapter加载更多配置
     *
     * @param adapter
     * @return
     */
    override fun createDefaultLoadMoreView(adapter: BaseQuickAdapter<*, *>?): LoadMoreView? {
        if (adapter != null) {
            //设置动画是否一直开启
            adapter.isFirstOnly(false)
            //设置动画
            adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
        }
        return FastLoadMoreView.Builder(mContext)
            .build()
    }

    /**
     * 多状态布局配置
     * @return
     */
    override fun createMultiStatusView(): IMultiStatusView {
        //根据具体情况可设置更多属性具体请参考FastMultiStatusView.Builder里set方法
        //默认设置请参考Builder(Context context)里初始化
        return FastMultiStatusView.Builder(mContext)
            .setLoadingProgressColor(mContext.getResources().getColor(R.color.mainColor))
            .build()
    }

    override fun createLoadingDialog(activity: Activity?): FastLoadDialog? {
        return FastLoadDialog(activity!!, UIProgressView.STYLE_MATERIAL_DESIGN)
            .setMessage("加载中,请稍候...")
    }

    override fun createHttpErrorControl(
        errorRes: Int,
        errorCode: Int, @io.reactivex.annotations.NonNull e: Throwable,
        context: Context,
        vararg args: Any?
    ): Boolean {
        LoggerManager.e(TAG, "args:" + args + ";context:" + context.javaClass.simpleName)
        if (args != null) {//可以将具体调用界面部分视图传递到全局控制
            if (args.size >= 5) {
                if (args[1] is SmartRefreshLayout) {
                    LoggerManager.e(TAG, "args:" + args[1])
                    (args[1] as SmartRefreshLayout).finishRefresh()
                }
                if (args[2] is BaseQuickAdapter<*, *>) {
                    LoggerManager.e(TAG, "args:" + args[2])
                    (args[2] as BaseQuickAdapter<*, *>).loadMoreComplete()
                }
                if (args[3] is EasyStatusView) {
                    LoggerManager.e(TAG, "args:" + args[3])
                    (args[3] as EasyStatusView).error()
                    if (args[4] as Int == 0) {
                        when (errorCode) {
                            FastError.EXCEPTION_ACCOUNTS -> {

                            }
                            FastError.EXCEPTION_JSON_SYNTAX -> {

                            }
                            FastError.EXCEPTION_OTHER_ERROR -> {

                            }
                            FastError.EXCEPTION_TIME_OUT -> {

                            }
                            else -> (args[3] as EasyStatusView).noNet()
                        }
                    } else {
                        //                        ToastUtil.show(errorRes);
                    }
                }
            }
        }
        //返回值true则FastObserver不会回调_onError所有逻辑处理都在全局位置处理
        return false
    }
}